package com.project.controller;

import com.project.bean.UserBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;

@RestController
@RequestMapping("test")
public class TestController {

    @RequestMapping("speak")
    public String speak(){
        return "ok";
    }

    @RequestMapping("getUser")
    public UserBean getUser(){
        return new UserBean(1,"张三",
                LocalDate.parse("1998-03-04"),20000);
    }

    @RequestMapping("add")
    public UserBean add(UserBean user){
        return user;
    }

    @RequestMapping("register")
    public String register(String name,
            @RequestParam("face") MultipartFile mf) throws IOException {
        //得到上传文件名
        String fileName = mf.getOriginalFilename();

        mf.transferTo(new File("d:/face/"+fileName));
        return "ok,"+name+","+fileName;
    }

    @RequestMapping("land")
    public String land(String userName, String pwd, HttpServletRequest request){
        if("java".equals(userName) && "123".equals(pwd)){
            request.getSession().setAttribute("loginUser",userName);
            return "登陆成功";
        }

        return "登陆失败";
    }

}
